package com.diancan;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.SqlLiteDB.MenuDataHelper;
import com.Utils.CustomViewBinder;
import com.Utils.FileUtils;
import com.Utils.MenuUtils;
import com.custom.Rotate3d;
import com.declare.Declare;
import com.mode.CategoryObj;
import com.mode.MenuObj;
import com.mode.SelectedMenuObj;
import com.model.AllDomain;
import com.model.Category;
import com.model.Desk;
import com.model.Recipe;

import android.R.integer;
import android.app.ListActivity;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class CategoryList extends ListActivity {
//	private List<SelectedMenuObj> Infos = null;
	Declare declare;	
	List<CategoryObj> m_arr;
	ArrayList<HashMap<String, Object>> hashlist;
	SimpleAdapter simpleAdapter;
	public int sWidth;
	int[] idarray=new int[]{R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,R.drawable.c3,R.drawable.c2,R.drawable.c1};
	public ExecutorService executorService = Executors
			.newFixedThreadPool(5);
	public Handler handler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categorylist);
		//屏幕尺寸容器
		DisplayMetrics dm;
		dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		sWidth = dm.widthPixels;
		declare=(Declare)getApplicationContext();	
		hashlist=new ArrayList<HashMap<String,Object>>();
		
		InitList();
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
//	private void parseByQuery(Cursor cursor,List<SelectedMenuObj> infos) {
//		while(cursor.moveToNext()){
//			String id=cursor.getString(cursor.getColumnIndex("id"));
//			String cid=cursor.getString(cursor.getColumnIndex("cid"));
//			String name = cursor.getString(cursor.getColumnIndex("name"));
//			String description = cursor.getString(cursor.getColumnIndex("description"));
//			double price=cursor.getDouble(4);
//			String image = cursor.getString(cursor.getColumnIndex("image"));
//			MenuObj menuInfo=new MenuObj();
//			menuInfo.setId(id);
//			menuInfo.setCid(cid);
//			menuInfo.setName(name);
//			menuInfo.setDescription(description);
//			menuInfo.setImage(image);
//			menuInfo.setPrice(price);
//			SelectedMenuObj sMenuInfo=new SelectedMenuObj(menuInfo);
//			infos.add(sMenuInfo);
//		}
//	}
	private void InitList()
	{
		m_arr=declare.getMenuListDataObj().getCategoryObjs();
		//添加搜索item
		HashMap<String, Object> map1=new HashMap<String, Object>();
		map1.put("name", "搜索");
		BitmapFactory.Options options1=new BitmapFactory.Options();
        options1.inSampleSize = 3;
		Bitmap bmpBitmap1=BitmapFactory.decodeResource(this.getResources(), R.drawable.c3,options1);
		bmpBitmap1=Bitmap.createScaledBitmap(bmpBitmap1, sWidth, 130, false);
		map1.put("img", bmpBitmap1);
		hashlist.add(map1);
		for(final CategoryObj categoryObj:m_arr)
		{
			
			executorService.submit(new Runnable() {
				 public void run() {
					 handler.post(new Runnable() {
						 public void run() {
							HashMap<String, Object> map=new HashMap<String, Object>();
							map.put("name", categoryObj.getName()); 
							Bitmap bmpBitmap=MenuUtils.GetCategoryBitMap(categoryObj.getImage());
							bmpBitmap=Bitmap.createScaledBitmap(bmpBitmap, sWidth, 130, false);
							map.put("img", bmpBitmap);
							hashlist.add(map);
						 }
					 });
				 }
			});
			
		}
		
		simpleAdapter=new SimpleAdapter(this, hashlist,
				R.layout.categorylistitem, new String[] { "name", "img" },
				new int[] { R.id.nameText,R.id.categoryimg});
		simpleAdapter.setViewBinder(new CustomViewBinder());
		setListAdapter(simpleAdapter);
		DisplayMenus();
	}
	private void DisplayMenus()
	{
        Rotate3d Animation1 = new Rotate3d(90, 0, 0.0f, 0.0f, sWidth/2, 65);   
        Animation1.setFillAfter(true);   
        Animation1.setDuration(200);
		LayoutAnimationController lAnimationcler=new LayoutAnimationController(Animation1);
		lAnimationcler.setOrder(LayoutAnimationController.ORDER_NORMAL);
		lAnimationcler.setDelay(0.5f);
		getListView().setLayoutAnimation(lAnimationcler);
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(position==0)
		{
			MenuGroup parent1 = (MenuGroup) getParent();
		    final LinearLayout contain1 = (LinearLayout) parent1.findViewById(R.id.group_Layout);
		    Animation sAnimation=AnimationUtils.loadAnimation(CategoryList.this, R.anim.push_left_out);
	        View view2=this.getWindow().getDecorView();
	        view2.startAnimation(sAnimation);
			contain1.removeAllViews();
			Intent in1 = new Intent(getParent(), SearchList.class);
			in1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			LocalActivityManager manager1 = parent1.getLocalActivityManager();
			Window window1 = manager1.startActivity("SearchList", in1);
			
			View view1=window1.getDecorView();		
			contain1.addView(view1);
			LayoutParams params1=(LayoutParams) view1.getLayoutParams();
	        params1.width=LayoutParams.FILL_PARENT;
	        params1.height=LayoutParams.FILL_PARENT;
	        view1.setLayoutParams(params1);
		}
		else {
			position--;

			MenuGroup parent = (MenuGroup) getParent();
		    final LinearLayout contain = (LinearLayout) parent.findViewById(R.id.group_Layout);
			contain.removeAllViews();
			Intent in = new Intent(getParent(), MenuFlipPage.class);
			in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			in.putExtra("rindex", "0");
			in.putExtra("index", position+"");
			LocalActivityManager manager = parent.getLocalActivityManager();
			String currentId = manager.getCurrentId();
			Window window = manager.startActivity("MenuFlipPage", in);
			
			View view=window.getDecorView();		
			contain.addView(view);
			LayoutParams params=(LayoutParams) view.getLayoutParams();
	        params.width=LayoutParams.FILL_PARENT;
	        params.height=LayoutParams.FILL_PARENT;
	        view.setLayoutParams(params);
	        
	        Animation sAnimation=AnimationUtils.loadAnimation(CategoryList.this, R.anim.open_in);
	        view.startAnimation(sAnimation);
		}
		
		super.onListItemClick(l, v, position, id);
	}
}
